package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.Type;
import cn.xbtop.blog.service.TypeService;
import cn.xbtop.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/10 21:57
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 添加分类
     * @param type
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Type type) {
        typeService.insert(type);

        return new Result<>("添加成功！");
    }

    /**
     * 后台查询所有分类
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<Object> list() {
        List<Type> list = typeService.findAll();
        return new Result<>(list);
    }

    /**
     * 前台查询所有分类
     *
     * @return
     */
    @RequestMapping(value = "/findlist", method = RequestMethod.GET)
    public Result<Object> findlist() {
        List<Type> list = typeService.findlist();
        return new Result<>(list);
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Type type) {
        typeService.updateByPrimaryKey(type);
        return new Result<>("修改成功！");
    }

    /**
     * 根据id启用分类
     * @return
     */
    @RequestMapping(value = "/enable/{id}",method = RequestMethod.PUT)
    public Result<Object> enable(@PathVariable Integer id){
        typeService.enableId(id);
        return new Result<>("启用成功！");
    }

    /**
     * 根据id弃用分类
     * @return
     */
    @RequestMapping(value = "/disenable/{id}",method = RequestMethod.PUT)
    public Result<Object> disenable(@PathVariable Integer id){
        typeService.disenableId(id);
        return new Result<>("弃用成功！");
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    // 路径中取 id
    public Result<Object> delete(@PathVariable Integer id){
        typeService.deleteByPrimaryKey(id);
        return new Result<>("删除成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
    public Result<Object> findById(@PathVariable Integer id){
        Type type = typeService.selectByPrimaryKey(id);
        return new Result<Object>(type);
    }
}
